package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.log.MethodLog;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IDefaultPrdSalService;
import com.wondertek.poms.dao.po.DefaultPrdSal;
import com.wondertek.poms.service.util.BeanPropertiesUtil;
import com.wondertek.poms.service.vo.DefaultPrdSalVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author evan0213
 * @description
 */
@RestController
@RequestMapping("default_prd_sal")
public class DefaultPrdSalController {
	@Autowired
	private IDefaultPrdSalService defaultPrdSalService;
	@Autowired
	private BeanPropertiesUtil beanPropertiesUtil;

	/**
	 * @param prdInfoId 商品包id
	 * @return 商品包的默认计费规则列表
	 */
	@ApiOperation(value = "商品包默认计费规则 列表", notes = "商品包默认计费规则 列表")
	@GetMapping("/list")
	@MethodLog(module = "商品包默认计费规则列表")
	public ResultBean list(String prdInfoId) {
		List<DefaultPrdSal> list = defaultPrdSalService.findAllByPrdInfoId(prdInfoId);
		return ResultBean.ok(list);
	}

	/**
	 * @param ids 待删除id，英文逗号拼接
	 * @return 删除结果
	 */
	@ApiOperation(value = "商品包默认计费规则 删除", notes = "商品包默认计费规则 删除")
	@DeleteMapping("/del")
	@MethodLog(module = "商品包默认计费规则删除", insertDB = true, opterate = 3)
	public ResultBean delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] idArr = ids.split(",");
			for (String id : idArr) {
				defaultPrdSalService.deleteById(Long.valueOf(id));
			}
			return ResultBean.ok();
		} else {
			return ResultBean.error("id不能为空");
		}

	}

	@PostMapping("/add")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "prdInfoId", required = true, value = "商品包id"),
			@ApiImplicitParam(name = "productId", required = true, value = "计费id"),
			@ApiImplicitParam(name = "firstClassId", required = false, value = "一级分类"),
			@ApiImplicitParam(name = "feeCharge", required = false, value = "资费类型"),
			@ApiImplicitParam(name = "formType", required = false, value = "内容类型"),
			@ApiImplicitParam(name = "firstPublish", required = false, value = "是否首发"),
			@ApiImplicitParam(name = "copyRightType", required = false, value = "版权类型"),
			@ApiImplicitParam(name = "contentLevel", required = false, value = "内容评级"),
			@ApiImplicitParam(name = "secondClass", required = false, value = "二级分类"),
			@ApiImplicitParam(name = "keyWord", required = false, value = "关键字")
	})
	@MethodLog(module = "商品包默认计费规则新增", insertDB = true, opterate = 1)
	public ResultBean add(@RequestBody DefaultPrdSalVo defaultPrdSalVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(defaultPrdSalVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(defaultPrdSalVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		DefaultPrdSal defaultPrdSal = new DefaultPrdSal();
		BeanUtils.copyProperties(defaultPrdSalVo, defaultPrdSal);
		DefaultPrdSal save = defaultPrdSalService.save(defaultPrdSal);
		return ResultBean.ok(save);
	}

	@PutMapping("/edit")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", required = true, value = "规则id"),
			@ApiImplicitParam(name = "prdInfoId", required = true, value = "商品包id"),
			@ApiImplicitParam(name = "productId", required = true, value = "计费id"),
			@ApiImplicitParam(name = "firstClassId", required = false, value = "一级分类"),
			@ApiImplicitParam(name = "feeCharge", required = false, value = "资费类型"),
			@ApiImplicitParam(name = "formType", required = false, value = "内容类型"),
			@ApiImplicitParam(name = "firstPublish", required = false, value = "是否首发"),
			@ApiImplicitParam(name = "copyRightType", required = false, value = "版权类型"),
			@ApiImplicitParam(name = "contentLevel", required = false, value = "内容评级"),
			@ApiImplicitParam(name = "secondClass", required = false, value = "二级分类"),
			@ApiImplicitParam(name = "keyWord", required = false, value = "关键字")
	})
	@MethodLog(module = "商品包默认计费规则编辑", insertDB = true, opterate = 2)
	public ResultBean edit(@RequestBody DefaultPrdSalVo defaultPrdSalVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(defaultPrdSalVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(defaultPrdSalVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		DefaultPrdSal defaultPrdSal = new DefaultPrdSal();
		BeanUtils.copyProperties(defaultPrdSalVo, defaultPrdSal);
		DefaultPrdSal result = defaultPrdSalService.update(defaultPrdSal);
		return ResultBean.ok(result);
	}

	private boolean isRepeat(DefaultPrdSalVo defaultPrdSalVo) {
		DefaultPrdSal temp = new DefaultPrdSal();
		CopyUtils.copyProperties(defaultPrdSalVo, temp, "prdRuleId");
		beanPropertiesUtil.setNullIfStringIsBlank(temp);
		List<DefaultPrdSal> list = defaultPrdSalService.findAll(Example.of(temp));
		for (DefaultPrdSal defaultPrdSal : list) {
			DefaultPrdSalVo vo = new DefaultPrdSalVo();
			DefaultPrdSalVo tempVo = new DefaultPrdSalVo();
			CopyUtils.copyProperties(defaultPrdSal, vo, "id");
			CopyUtils.copyProperties(defaultPrdSalVo, tempVo, "id");
			beanPropertiesUtil.setNullIfStringIsBlank(tempVo);
			beanPropertiesUtil.setNullIfStringIsBlank(vo);
			if (vo.equals(tempVo)) {
				if (defaultPrdSalVo.getId() == null || !Objects.equals(defaultPrdSalVo.getId(), defaultPrdSal.getId())) {
					return true;
				}
			}
		}
		return false;
	}
}
